package com.domain.controller;

import com.domain.common.AppLogger;
import com.domain.common.Comments;
import com.domain.common.Result;
import com.domain.service.IndexService;
import com.domain.utils.JwtUtils;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 首页
 */
@Controller
@RequestMapping("/index")
public class IndexControler {

    @Autowired
    private IndexService indexService;

    /**
     * 日志
     */
    private static Logger logger = AppLogger.getLogger(IndexControler.class.getSimpleName(), Level.INFO.toString(), true);

    /**
     * 获取首页
     *
     * @return
     */
    @RequestMapping(value = "/getIndexUsersInfo", method = RequestMethod.POST)
    @ResponseBody
    public Result getIndexUsersInfo(String userToken, String usersId, String pageNum, String pageSize, HttpServletRequest request) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            List<Map<String, Object>> maps = indexService.getIndexUsersInfo(usersId, pageNum, pageSize);
            return Result.success(maps);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 获取首页
     *
     * @return
     */
    @RequestMapping(value = "/getIndexUsersInfos", method = RequestMethod.POST)
    @ResponseBody
    public Result getIndexUsersInfos(String userToken, String usersId, String pageNum, String pageSize) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            List<Map<String, Object>> maps = indexService.getIndexUsersInfos(usersId, pageNum, pageSize);
            return Result.success(maps);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 获取首页(公益模块)
     *
     * @return
     */
    @RequestMapping(value = "/getIndexG", method = RequestMethod.POST)
    @ResponseBody
    public Result getIndexG(String userToken, String usersId, String pageNum, String pageSize, String type) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            List<Map<String, Object>> maps = indexService.getIndexG(usersId, pageNum, pageSize, type);
            return Result.success(maps);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }


    /**
     * 获取评论列表
     *
     * @param userToken 用户token
     * @param publishId 发布帮忙id
     * @param request
     * @return
     */
    @RequestMapping(value = "/getCommentPublishList", method = RequestMethod.POST)
    @ResponseBody
    public Result getCommentPublishList(String userToken, String publishId, String pageNum, String pageSize, HttpServletRequest request) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return indexService.getCommentPublishList(publishId, pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * @return
     */
    @RequestMapping(value = "/getOpens", method = RequestMethod.POST)
    @ResponseBody
    public Result getOpens() {
        try {
            return indexService.getOpens();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }


    /**
     * 添加评论
     *
     * @param userToken 用户token
     * @param userId    评论人id
     * @param content   评论内容
     * @param publishId 发布id
     * @param request
     * @return
     */
    @RequestMapping(value = "/addComment", method = RequestMethod.POST)
    @ResponseBody
    public Result addComment(String userToken, String userId, String content, String publishId, HttpServletRequest request) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return indexService.addComment(userId, content, publishId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 获取举报类型
     *
     * @param userToken 用户token
     * @return
     */
    @RequestMapping(value = "/getReportType", method = RequestMethod.POST)
    @ResponseBody
    public Result getReportType(String userToken) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return indexService.getReportType();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统失败");
        }
    }

    /**
     * 添加举报
     *
     * @param userToken    用户token
     * @param reportTypeId
     * @param content
     * @param images
     * @param publishId
     * @return
     */
    @RequestMapping(value = "/addReport", method = RequestMethod.POST)
    @ResponseBody
    public Result addReport(String userToken, String userId, String reportTypeId, String content, String images, String publishId, HttpServletRequest request) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return indexService.addReport(userId, reportTypeId, content, images, publishId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 添加关注
     *
     * @param userToken 用户token
     * @param userId    用户id
     * @param publishId 关注得发布id
     * @return
     */
    @RequestMapping(value = "/addFollow", method = RequestMethod.POST)
    @ResponseBody
    public Result addFollow(String userToken, String userId, String publishId, String helpUsersId) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return indexService.addFollow(userId, publishId, helpUsersId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 获取我的关注列表
     *
     * @param userToken
     * @param userId
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUsersFollowList", method = RequestMethod.POST)
    @ResponseBody
    public Result getUsersFollowList(String userToken, String userId, String pageNum, String pageSize, HttpServletRequest request) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return indexService.getUsersFollowList(userId, pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 取消关注
     *
     * @param userToken 用户token
     * @return
     */
    @RequestMapping(value = "/removeFollow", method = RequestMethod.POST)
    @ResponseBody
    public Result removeFollow(String userToken, String usersId, String publishId) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return indexService.removeFollow(usersId, publishId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 筛选
     *
     * @param usersId
     * @param userToken   用户token
     * @param publishType 发布类型id（多个）
     * @param city        城市（多个）
     * @param gender      性别（1 男  2 女）
     * @param itisPaid    是否有偿（1 有偿  2 无偿）
     * @return
     */
    @RequestMapping(value = "/serch", method = RequestMethod.POST)
    @ResponseBody
    public Result serch(String usersId, String userToken, String publishType, String city, String gender, String itisPaid, String pageNum, String pageSize) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return indexService.serch(usersId, publishType, city, gender, itisPaid, pageNum, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }


    /**
     * 帮转
     *
     * @param userToken 用户token
     * @param helpId    帮转人id
     * @param publishId 发布帮忙id
     * @param userId    发布人id
     * @param helpType  帮转类型  帮转类型（1 人脉圈  2 通讯录  3 微信  4 微博）
     * @return
     */
    @RequestMapping(value = "/helpPublish", method = RequestMethod.POST)
    @ResponseBody
    public Result helpPublish(String userToken, String helpId, String publishId, String userId, String helpType, HttpServletRequest request) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return indexService.helpPublish(helpId, publishId, userId, helpType);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 受理
     *
     * @param helpId            帮转人id
     * @param userToken         用户token
     * @param acceptanceId      受理人id
     * @param publishId         发布帮忙id
     * @param userId            发布人id
     * @param acceptanceMessage 受理留言
     * @return
     */
    @RequestMapping(value = "/publishAcceptance", method = RequestMethod.POST)
    @ResponseBody
    public Result publishAcceptance(String userToken, String acceptanceId, String publishId, String userId, String acceptanceMessage, String helpId, HttpServletRequest request) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return indexService.publishAcceptance(acceptanceId, publishId, userId, acceptanceMessage, helpId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统失败");
        }
    }

    /**
     * 保存选择城市记录
     *
     * @param userToken 用户token
     * @param usersId   用户id
     * @param cityName  城市名称
     * @param request
     * @return
     */
    @RequestMapping(value = "/addCityHistory", method = RequestMethod.POST)
    @ResponseBody
    public Result addCityHistory(String userToken, String usersId, String cityName, HttpServletRequest request) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return indexService.addCityHistory(usersId, cityName);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

    /**
     * 获取用户选择城市列表历史（时间倒叙 12条）
     *
     * @param userToken
     * @param usersId
     * @param request
     * @return
     */
    @RequestMapping(value = "/getUsersCityHistory", method = RequestMethod.POST)
    @ResponseBody
    public Result getUsersCityHistory(String userToken, String usersId, HttpServletRequest request) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return indexService.getUsersCityHistory(usersId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }

}
